/**
 * 
 */
package com.metrology.app.dao.common;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;

public class CommonClientDaoSupport extends SqlSessionDaoSupport{
	
	public static final String POSTFIX_INSERT = ".insert";

	public static final String POSTFIX_UPDATE = ".update";
	
	public static final String POSTFIX_UPDATE_WITHOUT_FLUSH = ".updateWithoutFlush";

	public static final String POSTFIX_DELETE_PRIAMARYKEY = ".deleteByPrimaryKey";
	
	public static final String POSTFIX_REMOVE_BY_FKEY = ".removeByFKey";
	
	public static final String POSTFIX_REMOVE_ALL = ".removeAll";
	
	public static final String POSTFIX_LOGIC_DELETE = ".logicDelete";
	
	public static final String POSTFIX_LOGIC_DELETE_BY_FKEY = ".logicDeleteByFKey";

	public static final String POSTFIX_SELECT = ".load";

	public static final String POSTFIX_GETALL = ".getAll";
	
	public static final String POSTFIX_FINDDOMAINS = ".findDomains";
	
	public static final String POSTFIX_FIND_DOMAIN = ".findDomain";
	
	public static final String POSTFIX_FIND_OBJECT = ".findObject";
	
	@SuppressWarnings("unchecked")
	public <T> T _load(Class<T> entityClass, Long id) {
		
		Object obj = getSqlSession().selectOne(
				getStatementId(entityClass,
						CommonClientDaoSupport.POSTFIX_SELECT), id);

		return (T) obj;
	}

	/**
	 * 
	 * insert 向数据库中插入一条记录
	 * 
	 * @param obj
	 * 
	 *            void
	 * @exception
	 */
	public void _insert(Object obj) {
		getSqlSession().insert(
				getStatementId(obj.getClass(),
						CommonClientDaoSupport.POSTFIX_INSERT), obj);
	}

	/**
	 * 
	 * update
	 * 
	 * @方法作用 更新当前对象
	 * 
	 * @param obj
	 * 
	 *            void
	 * @exception
	 */
	public void _update(Object obj) {
		getSqlSession().update(
				getStatementId(obj.getClass(),
						CommonClientDaoSupport.POSTFIX_UPDATE), obj);
	}
	
	public void _updateBySqlId(Object obj, String sqlId) {
		getSqlSession().update(
				getStatementId(obj.getClass(),
						sqlId), obj);
	}

	/**
	 * 
	 * remove
	 * 
	 * @方法作用 根据ID删除某一个对象
	 * 
	 * @param entityClass
	 * @param uuid
	 * 
	 *            void
	 * @exception
	 */
	public <T> void _remove(Class<T> entityClass, Long uuid) {
		getSqlSession().delete(
				getStatementId(entityClass,
						CommonClientDaoSupport.POSTFIX_DELETE_PRIAMARYKEY),
				uuid);
	}
	
	public <T> void _removeByFKey(Class<T> entityClass, Long fKey) {
		getSqlSession().delete(
				getStatementId(entityClass,
						CommonClientDaoSupport.POSTFIX_REMOVE_BY_FKEY),
						fKey);
	}
	
	/**
	 * 
	 * remove
	 * 
	 * @方法作用 根据ID删除某一个对象
	 * 
	 * @param entityClass
	 * @param uuid
	 * 
	 *            void
	 * @exception
	 */
	public <T> void _rogicDelete(Class<T> entityClass, Long uuid) {
		getSqlSession().delete(
				getStatementId(entityClass,
						CommonClientDaoSupport.POSTFIX_LOGIC_DELETE),
				uuid);
	}
	
	/**
	 * 
	 * _rogicDeleteByFKey
	 * 
	 * @方法作用 根据ID删除某一个对象
	 * 
	 * @param entityClass
	 * @param fkey
	 * 
	 *            void
	 * @exception
	 */
	public <T> void _rogicDeleteByFKey(Class<T> entityClass, Long fkey) {
		getSqlSession().delete(
				getStatementId(entityClass,
						CommonClientDaoSupport.POSTFIX_LOGIC_DELETE_BY_FKEY),
						fkey);
	}

	public <T> List<T> _findAll(Class<T> entityClass) {
		return getSqlSession().selectList(
				getStatementId(entityClass, POSTFIX_GETALL));
	}
	
	public <T> List<T> _findAll(Map<String, Object> params, String queryName) {
		return getSqlSession().selectList(
				queryName, params);
	}

	/**
	 * 
	 * getStatementId
	 * 
	 * @方法作用 获取Mapping文件中的Id， mapping文件的命名格式为： classname.suffix.
	 * 
	 * @param entityClass
	 * @param suffix
	 * @return
	 * 
	 *         String
	 * @exception
	 */

	protected <T> String getStatementId(Class<T> entityClass, String suffix) {
		String className = entityClass.getSimpleName();
		return className + suffix;
	}
	
	public <T> List<T> _findDomains(Class<T> domainClass,
			Map<String, Object> parameters) {
		return getSqlSession().selectList(
				getStatementId(domainClass, POSTFIX_FINDDOMAINS ), parameters);
	}

	/* (non-Javadoc)  
	 * @see com.djb.dao.DomainObjectDao#findDomain(java.lang.String, java.util.Map)  
	 */
	@SuppressWarnings("unchecked")
	public <T> T _findDomain(Class<T> domainClass,
			Map<String, Object> parameters){
		return (T) getSqlSession().selectOne(
				getStatementId(domainClass, POSTFIX_FIND_DOMAIN ), parameters);
	}

	/* (non-Javadoc)  
	 * @see com.djb.dao.DomainObjectDao#findField(java.lang.String, java.util.Map)  
	 */
	public <T> Object _findObject(Class<T> domainClass, Map<String, Object> parameters) {
		return getSqlSession().selectOne(
				getStatementId(domainClass, POSTFIX_FIND_OBJECT ), parameters);
	}

	/**  
	 * removeAll
	 * @方法作用
	 * 删除所有历史记录
	 *  
	 * @param clazz
	 *   
	 * void  
	 * @exception   
	*/
	public void removeAll(Class<?> clazz) {
		getSqlSession().delete(
				getStatementId(clazz, POSTFIX_REMOVE_ALL ));
	}

	
}
